package com.linked.basemanage.pass.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.linked.basemanage.config.RedisKeysConfig;
import com.linked.basemanage.pass.bean.param.AddPassInfoParam;
import com.linked.basemanage.pass.service.IPassHandleService;
import com.linked.basemanage.util.HandleRedisUtils;
import com.linked.universal.common.LinkedResult;
import com.linked.universal.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author :dbq
 * @date : 2023/3/24 14:55
 * @description : desc
 */
@RestController
@RequestMapping("passhandle")
@Slf4j
public class PassHandleController {


    private final ObjectMapper mapper;

    private final IPassHandleService passHandleService;

    private final HandleRedisUtils redisUtils;

    @Autowired
    public PassHandleController(ObjectMapper mapper, IPassHandleService passHandleService, HandleRedisUtils redisUtils) {
        this.mapper = mapper;
        this.passHandleService = passHandleService;
        this.redisUtils = redisUtils;
    }


    @PostMapping("addPassInfo")
    Result addPassInfo(@RequestBody AddPassInfoParam param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("增加通过信息接口 入参：{}", mapper.writeValueAsString(param));
        }
        boolean ret = false;

        try {
            ret = passHandleService.addPassInfo(param);
            if (ret) {
                if (1 == param.getPassType()) {
                    redisUtils.removeKey(RedisKeysConfig.KEY_WIHTELIST);
                } else if (2 == param.getPassType()) {
                    redisUtils.removeKey(RedisKeysConfig.KEY_BLACKLIST);
                }
            }
        } catch (Exception e) {
            log.error("增加通过信息接口 异常", e);
            return Result.error("未知异常，请联系管理员！");
        }
        return ret ? Result.success() : Result.ok(false, "添加失败！");
    }

//    @PostMapping(value = "queryPassList")
//    LinkedResult queryPassList(Integer passType) throws Exception {
//        if (log.isInfoEnabled()) {
//            log.info("查询通过信息接口 入参：{}", mapper.writeValueAsString(passType));
//        }
//        List<String> ret = null;
//
//        try {
//            switch (passType) {
//                case 2:
//                    ret = redisUtils.getRedisValueList(RedisKeysConfig.KEY_BLACKLIST, () -> passHandleService.queryBlackList());
//                    break;
//                case 1:
//                    ret = redisUtils.getRedisValueList(RedisKeysConfig.KEY_WIHTELIST, passHandleService::queryWihteList);
//                    break;
//                default:
//                    ret = null;
//            }
//
//        } catch (Exception e) {
//            log.error("增加通过信息接口 异常", e);
//            return LinkedResult.Error("未知异常，请联系管理员！");
//        }
//        return !CollectionUtils.isEmpty(ret) ? LinkedResult.Success(ret) : LinkedResult.OK(false, "未查询到数据");
//    }
}
